#!/bin/sh

usage="
Download weather information from the AU BoM
Usage: $0 [options]
Options:
	-u, --update 		Update data sources (preceeds other options)
	-r, --rain_y		Yesterday's rainfall
	--rain_fig_y		Yesterday's rainfall figures
	--rain_t		Today's rainfall
	--rain_fig_t		Today's rainfall figures
	--rain_n		Rainfall over last hour 
	--rain_ani		Animation of last 6 hours rainfall
	-l, --radar		Radar loop
	-f, --forecast		Local forecast
	--fire			State fire danger ratings
	--forest_fire		Forest fire danger heatmap
	--grass_fire		Grass fire danger heatmap
	--national		National weather

See ftp://ftp2.bom.gov.au/anon/gen/README for more information."

# BoM ftp details
host='ftp2.bom.gov.au'			# Remote host
rdir='/anon/gen/'			# Remote ftp base directory
ldir="$HOME/.bom_data/"			# Directory to keep downloaded data

# Programs
pager='/usr/bin/less'			# Text pager
viewer='/usr/local/bin/display'		# Image viewer

local_forecast='fwo/IDV10716.txt'	# Mt Dandenong Forecast
national_weather='fwo/IDV17300.txt'	# National weather forecast

radar_id='IDR022'			# BoM radar image set
radar_mask="radar/${radar_id}.T.*.png"	# Images to include in the loop
radar_bg="${radar_id}.background.png"	# Image generated by layering the following list
radar_bg_list="radar_transparencies/${radar_id}.background.png
radar_transparencies/${radar_id}.topography.png
radar_transparencies/${radar_id}.waterways.png
radar_transparencies/${radar_id}.locations.png"

# Rainfall images
rain_old='fwo/IDV65316.gif'		# Rainfall Yesterday (till 9am)
rain_new='fwo/IDV65317.gif'		# Rainfall Today (since 9am)
rain_1hr='fwo/IDV65318.gif'		# Rainfall Now (last hour)
rain_old_fig='fwo/IDV65323.gif'		# Rainfall Yesterday (location figures till 9am)
rain_new_fig='fwo/IDV65324.gif'		# Rainfall Today (since 9am)
rain_1hr_fig='fwo/IDV65325.gif'		# Rainfall Now (last hour)
rain_2hr_fig='fwo/IDV65326.gif'		# 1hr earlier
rain_3hr_fig='fwo/IDV65327.gif'		# 2hrs earlier
rain_4hr_fig='fwo/IDV65328.gif'		# 3hrs earlier
rain_5hr_fig='fwo/IDV65329.gif'		# 4hrs earlier
rain_6hr_fig='fwo/IDV65330.gif'		# 5hrs earlier

state_fire='fwo/IDV18555.txt'		# Victorian fire danger ratings
state_forest_fire='fwo/IDV65406.png'	# Victorian forest fire danger heatmap
state_grass_fire='fwo/IDV65426.png'	# Victorian grass fire danger heatmap


get() {
	[ ! -d "$ldir" ] && mkdir "$ldir"
	[ ! -d "$ldir$(dirname "$1")" ] && mkdir "$ldir$(dirname "$1")"

	if [ -n "$update" ]
	then
		rfile="$rdir$1"
		lfile="$ldir$1"
		if [ -f "$lfile" ] 
		then
			ftp -ain "$host" <<- .
				quote USER anonymous
				quote PASS no@email.com
	 			newer "$rfile" "$lfile"
			.
		else
			ftp -ain "$host" <<- .
				quote USER anonymous
				quote PASS no@email.com
	 			get "$rfile" "$lfile"
			.
		fi
	fi
}

viewer() {
	for file in $*
	do
		if [ ! -f "$file" ]
		then echo "$file not found"
		else $viewer "$*"
		fi
	done
}

pager() {
	for file in $*
	do
		if [ ! -f "$file" ]
		then echo "$file not found"
		else $pager "$*"
		fi
	done
}

mkradar() {
	# Make the background map
	if [ ! -f "$ldir$radar_bg" ]
	then
		for image in $radar_bg_list
		do
			get "$image"
			if [ -z "$bg" ]
			then
				bg="$image"; continue
			else
				composite "$ldir$image" "$ldir$bg" "$ldir$bg"
			fi
		done
		mv "$ldir$bg" "$ldir$radar_bg" && rm -rf "$(dirname "$ldir$bg")"
	fi

	# Get the images
	#TODO: Somehow use the get() function instead of wget
	[ -d "$ldir$(dirname "$radar_mask")" ] && rm -r "$ldir$(dirname "$radar_mask")"
	wget -nv -P "$ldir$(dirname "$radar_mask")" "ftp://${host}$rdir$radar_mask"

	# Make the animation frames
	image_list="$(find "$ldir" -iname "$(basename "$radar_mask")")"
	if [ -n "$image_list" ]
	then
		for image in $image_list
		do
			composite "$image" "$ldir$radar_bg" "$image"
		done
	fi
}

#[ -z "$1" ] && pager "$ldir$local_forecast"
[ -z "$1" ] && echo "$usage"
while [ "$#" -ne '0' ]
do
	arg="$1"
	shift
	case "$arg" in
		'-u'|'--update') update=1 ;;
		'-f'|'--forecast')
			get "$local_forecast"
			pager "$ldir$local_forecast"
		;;
		'--national')
			get "$national_weather"
			pager "$ldir$national_weather"
		;;
		'-r'|'--rain_y')
			get "$rain_old"
			viewer "$ldir$rain_old"
		;;
		'--rain_t')
			get "$rain_new"
			viewer "$ldir$rain_new"
		;;
		'--rain_n')
			get "$rain_1hr"
			viewer "$ldir$rain_1hr"
		;;
		'--rain_fig_y')
			get "$rain_old_fig"
			viewer "$ldir$rain_old_fig"
		;;
		'--rain_fig_t')
			get "$rain_new_fig"
			viewer "$ldir$rain_new_fig"
		;;
		'--rain_ani')
			get "$rain_1hr_fig"
			get "$rain_2hr_fig"
			get "$rain_3hr_fig"
			get "$rain_4hr_fig"
			get "$rain_5hr_fig"
			get "$rain_6hr_fig"
			animate -delay 100 "$ldir$rain_1hr_fig" "$ldir$rain_2hr_fig" "$ldir$rain_3hr_fig" "$ldir$rain_4hr_fig" "$ldir$rain_5hr_fig" "$ldir$rain_6hr_fig"
		;;
		'-l'|'--radar')
			# Download and build animation
			[ -n "$update" ] && mkradar
			# Play the animation
			[ -d "$ldir$(dirname "$radar_mask")" ] && animate -delay 100 "$ldir$radar_mask"
		;;
		'--fire')
			get "$state_fire"
			pager "$ldir$state_fire"
		;;
		'--forest_fire')
			get "$state_forest_fire"
			viewer "$ldir$state_forest_fire"
		;;
		'--grass_fire')
			get "$state_grass_fire"
			viewer "$ldir$state_grass_fire"
		;;
		*)
			echo "$usage"
			exit
		;;
	esac
done
